import CryptoJS from 'crypto-js'

const aseKey = 'mark-system12345' // 秘钥必须为：8/16/32位
const message = '123456'

// console.log(decrypt('voTHsSdr1dcQsO2TVttayN/0eYYV7BCkLbcQ58KNoDo='))
export function encrypt(word) {
  let encrypted = ''
  if (typeof (word) === 'string') {
    encrypted = CryptoJS.AES.encrypt(word, CryptoJS.enc.Utf8.parse(aseKey), {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
  } else if (typeof word === 'object') { // 对象格式的转成json字符串
    const data = JSON.stringify(word)
    encrypted = CryptoJS.AES.encrypt(data, CryptoJS.enc.Utf8.parse(aseKey), {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
  }
  return encrypted.toString()
}
// 解密函數
export function decrypt(word) {
  const decrypt = CryptoJS.AES.decrypt(word, CryptoJS.enc.Utf8.parse(aseKey), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return decrypt.toString(CryptoJS.enc.Utf8)
}
